<?php
/**
 * (model) sku预估（EU国家）
 */
namespace App\Models\SsBasic;

use Illuminate\Database\Eloquent\Model;
class CountrySkuEstimate extends Model
{
    protected $table = 's_country_sku_estimate';

    protected $guarded = [];

    const ESTIMATE_NUM = 18;

    const NEW_ESTIMATE_NUM = 9; //预估月份（9个月）

    /**
     * 获取预估数组
     * @param $model
     * @return array
     */
    public static function getEstimate($model){
        $num = self::ESTIMATE_NUM;
        $estimate = [];
        for($i = 0; $i < $num; $i++){
            $estimate['estimate_num'.$i] = ($model->{'estimate_num'.$i}) ?? 0;
        }
        return $estimate;
    }

    /**
     * 获取库存系数
     * @param       $inventory
     * @param array $estimateNum
     * @return float|int|string
     */
    public static function getInventoryFactor($inventory,$estimateNum = []){
        $num = self::ESTIMATE_NUM - 1;
        $sum = 0;
        $k = 0;
        $count = 0;
        for($i = 0; $i <= $num; $i++){
            $sum += $estimateNum['estimate_num'.$i];
            if($inventory <= $sum){
                $k = $i;
                break;
            }
            $count++;
        }
        if($count == self::ESTIMATE_NUM){
            if($inventory == $sum){
                $factor = self::ESTIMATE_NUM;
            }else{
                $factor = self::ESTIMATE_NUM.'+';
            }
        }else{
            $sum2 = 0;
            $factor = 0;
            for ($j = 0; $j < $k; $j++){
                $sum2 += $estimateNum['estimate_num'.$j];
                $factor++;
            }
            if($estimateNum['estimate_num'.$k] == 0){
                $factor += 0;
            }else{
                $factor += round(($inventory - $sum2) / $estimateNum['estimate_num'.$k],3);
            }
        }
        return $factor;
    }
}
